package com.chazz.service.impl;

import com.chazz.dao.CartDao;
import com.chazz.dao.GoodDao;
import com.chazz.dao.ShoppingcarDao;
import com.chazz.dao.Sku2Dao;
import com.chazz.dao.impl.CartDaoImpl;
import com.chazz.dao.impl.GoodDaoImpl;
import com.chazz.dao.impl.ShoppingcarDaoImpl;
import com.chazz.dao.impl.Sku2DaoImpl;
import com.chazz.entity.*;
import com.chazz.service.CartService;
import com.chazz.utils.DBUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartServiceImpl implements CartService {
    private CartDao cartDaoImpl = new CartDaoImpl();
    private CartDaoImpl cartDao = new CartDaoImpl();
    private ShoppingcarDao shoppingcarDaoImpl = new ShoppingcarDaoImpl();
    private GoodDao goodsDaoImpl = new GoodDaoImpl();
    private Sku2Dao skuDaoImpl = new Sku2DaoImpl();
    private List<Cart> carts = cartDao.getCarts();


    public void cartDelete(Cart cart){
        cartDaoImpl.delete(cart);
    }
    @Override
    public void CartGoodAdd(Integer goodsid, Integer userid) {
        cartDaoImpl.cartGoodAdd(goodsid,userid);
    }

    @Override
    public List<Cart> findCartBySid(Integer sid) {

        return cartDaoImpl.findCartBySid(sid);
    }

//    @Override
//    public Map<Goods, Integer> findGoodsByUserId(Integer userid) {
//        Map<Goods,Integer> goodmap = new HashMap<>();
//        //创建一个商品集合来存放购物车商品
////        List<Goods> goods = new ArrayList<Goods>();
//        //通过userid找到购物车
//        Shoppingcar shoppingcar = shoppingcarDaoImpl.findByUid(userid);
//        List<Cart> carts = cartDaoImpl.findCartBySid(shoppingcar.getId());
//        for(Cart c : carts){
//            Goods good = goodsDaoImpl.findCartByCartSid(c.getSid());
////            goods.add(good);
//            goodmap.put(good,c.getCount());
//        }
////        return goods;
//        return goodmap;
//    }

    /**
     * 通过uerid找到购物车前端需要的数据
     * @param userid
     * @return
     */
    @Override
    public List<CartDetails> findGoodsByUserId(Integer userid) {
        List<CartDetails> cartDetails = new ArrayList<>();
        Shoppingcar shoppingcar = shoppingcarDaoImpl.findByUid(userid);
        List<Cart> carts = cartDaoImpl.findCartBySid(shoppingcar.getId());
        for(Cart c : carts){
            CartDetails cartDetail = new CartDetails();
            cartDetail.setUserid(shoppingcar.getUid());
            cartDetail.setCart(c);
            cartDetail.setGood(goodsDaoImpl.findCartByCartSid(c.getGid()));
            cartDetail.setCount(c.getCount());
            cartDetail.setPrice(skuDaoImpl.findByGid(c.getGid()).getPrice());
            cartDetails.add(cartDetail);
        }

        return cartDetails;
    }


//    public Map<Goods,Integer> findGoodsCount(Integer userid){
//        Map<Goods,Integer> goodmap = new HashMap<Goods,Integer>();
//        List<Goods> goods = findGoodsByUserId(userid);
//        for(int i = 0;i<goods.size();i++){
//            Integer count = 0;
//            for(int j = 0;j<goods.size();j++){
//                if(goods.get(i) != goods.get(j)){
//                    continue;
//                }else{
//                    count++;
//                    break;
//                }
//            }
//            goodmap.put(goods.get(i),count);
//        }
//        return goodmap;
//    }


    @Override
    public void deleteCart(Integer id) {
        cartDaoImpl.delete(cartDaoImpl.findById(id));
    }

    @Override
    public void deleteByGid(Integer gid) {
        cartDaoImpl.deleteByGid(gid);
    }

    @Override
    public Cart findById(Integer id) {
        return cartDaoImpl.findById(id);
    }

    @Override
    public void updateCartCount(Integer count, Integer cartid) {
        cartDaoImpl.updateCartCount(count,cartid);
    }

    @Override
    public void deleteBySid(Integer sid){
        String sql = "DELETE FROM Cart WHERE sid=?";
        DBUtils.executeUpdate(sql,sid);
    }
    //未登录时购物车
    public void cartAdd(Integer goodid){
        cartDaoImpl.cartGoodAdd(goodid);
    }

    public void deleteCount(Integer gid){
//        for(Cart c : carts){
//            if(c.getGid()==gid){
//                c.setCount(c.getCount()-1);
//                if(c.getCount()<=0){
//                    delete(c.getGid());
//                }
//            }
//        }
        for(int i = 0;i<carts.size();i++){
            if(carts.get(i).getGid()==gid){
                carts.get(i).setCount(carts.get(i).getCount()-1);
                if(carts.get(i).getCount()<=0){
                    delete(gid);
                    i--;
                }
            }
        }

    }
    public void deleteAll(){
        carts.clear();
    }

    public void delete(Integer gid){

        for(Cart c : carts){
            if(c.getGid()==gid){
                carts.remove(c);
                return;
            }
        }

//        for(int i = 0;i<carts.size();i++){
//            if(carts.get(i).getGid()==gid){
//                carts.remove(i);
//                i--;
//            }
//        }
    }

    public List<CartDetails> findDetailsByGid(){
        List<CartDetails> cartDetails = new ArrayList<>();
        for(Cart c : carts){
            CartDetails cartDetail = new CartDetails();
            cartDetail.setUserid(0);
            cartDetail.setCart(c);
            cartDetail.setGood(goodsDaoImpl.findCartByCartSid(c.getGid()));
            cartDetail.setCount(c.getCount());
            cartDetail.setPrice(skuDaoImpl.findByGid(c.getGid()).getPrice());
            cartDetails.add(cartDetail);
        }
        return cartDetails;
    }
}
